package com.luo.chengrui.module.sa.controller.admin.commontree;

import com.luo.chengrui.module.sa.convert.commontree.CommonTreeConvert;
import com.luo.chengrui.module.sa.dal.dataobject.commontree.CommonTreeDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.commontree.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.commontree.CommonTreeBizDO;
import com.luo.chengrui.module.sa.convert.commontree.CommonTreeBizConvert;
import com.luo.chengrui.module.sa.service.commontree.CommonTreeBizService;

@Tag(name = "管理后台 - 业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用")
@RestController
@RequestMapping("/sa/common-tree-biz")
@Validated
public class CommonTreeBizController {

    @Resource
    private CommonTreeBizService commonTreeBizService;

    @PostMapping("/create")
    @Operation(summary = "创建业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:create')")
    public CommonResult<String> createCommonTreeBiz(@Valid @RequestBody CommonTreeBizCreateReqVO createReqVO) {
        return success(commonTreeBizService.createCommonTreeBiz(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:update')")
    public CommonResult<Boolean> updateCommonTreeBiz(@Valid @RequestBody CommonTreeBizUpdateReqVO updateReqVO) {
        commonTreeBizService.updateCommonTreeBiz(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:delete')")
    public CommonResult<Boolean> deleteCommonTreeBiz(@RequestParam("id") String id) {
        commonTreeBizService.deleteCommonTreeBiz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:query')")
    public CommonResult<CommonTreeBizRespVO> getCommonTreeBiz(@RequestParam("id") String id) {
        CommonTreeBizDO commonTreeBiz = commonTreeBizService.getCommonTreeBiz(id);
        return success(CommonTreeBizConvert.INSTANCE.convert(commonTreeBiz));
    }

    @GetMapping("/list")
    @Operation(summary = "获得业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:query')")
    public CommonResult<List<CommonTreeBizRespVO>> getCommonTreeBizList(@RequestParam("ids") Collection<String> ids) {
        List<CommonTreeBizDO> list = commonTreeBizService.getCommonTreeBizList(ids);
        return success(CommonTreeBizConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用分页")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:query')")
    public CommonResult<PageResult<CommonTreeBizRespVO>> getCommonTreeBizPage(@Valid CommonTreeBizPageReqVO pageVO) {
        PageResult<CommonTreeBizDO> pageResult = commonTreeBizService.getCommonTreeBizPage(pageVO);
        return success(CommonTreeBizConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用 Excel")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:export')")
    @OperateLog(type = EXPORT)
    public void exportCommonTreeBizExcel(@Valid CommonTreeBizExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CommonTreeBizDO> list = commonTreeBizService.getCommonTreeBizList(exportReqVO);
        // 导出 Excel
        List<CommonTreeBizExcelVO> datas = CommonTreeBizConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "业务树 业务端通用树结构，必须与SA_COMMON_TREE表数据合并使用.xls", "数据", CommonTreeBizExcelVO.class, datas);
    }

    @GetMapping("/all")
    @Operation(summary = "获得通用树分页")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:query')")
    public CommonResult<List<CommonTreeBizRespVO>> getCommonTreeAllList(@Valid CommonTreeBizExportReqVO pageVO) {
        List<CommonTreeBizDO> pageResult = commonTreeBizService.getCommonTreeAllList(pageVO);
        return success(CommonTreeBizConvert.INSTANCE.convertList(pageResult));
    }

    @GetMapping("/children")
    @Operation(summary = "查询子节点 ")
    @PreAuthorize("@ss.hasPermission('sa:common-tree-biz:query')")
    @OperateLog(type = EXPORT)
    public CommonResult<List<CommonTreeBizRespVO>> queryCommonTreeListByParentId(@RequestParam("id") String id) throws IOException {
        List<CommonTreeBizDO> list = commonTreeBizService.getChildrenBy(id);
        return success(CommonTreeBizConvert.INSTANCE.convertList(list));
    }

}
